package mn.more.mock.gen;

import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author $Author: mikeliucc $
 * @version $Revision: 119 $
 */
public class DateGenerator extends GeneratorBase {

	private DateFormat format;
	private Date startDate;
	private Date endDate;

	public DateFormat getFormat() { return format; }

	public void setFormat(DateFormat format) { this.format = format; }

	public Date getStartDate() { return startDate; }

	public void setStartDate(Date startDate) { this.startDate = startDate; }

	public Date getEndDate() { return endDate; }

	public void setEndDate(Date endDate) { this.endDate = endDate; }

	public List<String> generate() throws IOException {
		List<String> dates = new ArrayList<String>(max);

		int diff =
			(int) ((endDate.getTime() - startDate.getTime()) /
			       (1000 * 60 * 60 * 24));
		Calendar calendar = Calendar.getInstance();
		Random rand = new Random(System.currentTimeMillis());
		for (int i = 0; i < max; i++) {
			calendar.setTime(startDate);
			calendar.add(Calendar.DAY_OF_YEAR, rand.nextInt(diff));
			String date = format.format(calendar.getTime());
			if (isDataUsable(dates, date)) {
				dates.add(date);
			} else {
				i--;
			}
		}

		return dates;
	}
}
